"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 资源池化
Case Name   : 节点A读取页面P1，节点A和B共享内存中无P1
Create At   : 2023.5
Owner       : opentestcase009
Description :
    1、查看集群状态
    2、创建表,插入数据
    3、重启数据库
    4、创建视图
    5、节点A读取页面P1,节点A和B共享内存中无P1（主节点查询）
    6、对数据做修改后进行查询
    7、清理环境
Expect      :
    1、集群状态正常,数据库节点状态正常
    2、建表成功,插入数据成功
    3、重启数据库成功
    4、创建视图成功
    5、成功（内存中无数据，从磁盘获取）
    6、修改成功，查询成功
    7、清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.t_name = 't_dms_page_011'
        self.v_name = 'v_dms_page_011'

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:创建表,插入数据; expect:建表成功,插入数据成功-----'
        self.log.info(text)

        sql = f"create table {self.t_name}(id int,name varchar,salary int) " \
              f"with(segment=on);" \
              f"insert into {self.t_name} values(generate_series(20,50)," \
              f"'local_'||generate_series(20,50),60000);" \
              f"select count(*) from {self.t_name};"
        self.log.info(sql)
        res = self.comsh.execut_db_sql(sql)
        self.log.info(res)
        self.assertTrue(self.constant.CREATE_TABLE_SUCCESS in res
                        and self.constant.INSERT_SUCCESS_MSG in res,
                        '执行失败' + text)
        self.assertEqual('31', res.splitlines()[-2].strip(), '执行失败' + text)

        text = '-----step3:重启数据库; expect:重启成功-----'
        self.log.info(text)

        stop = self.comsh.exec_cm_ctl('stop')
        self.log.info(stop)
        start = self.comsh.exec_cm_ctl('start')
        self.log.info(start)

        self.assertIn(self.constant.cm_start_success_msg, start, '重启失败')

        text = '-----step4:创建视图; expect:创建成功-----'
        self.log.info(text)

        view = f'create view {self.v_name} as select * from {self.t_name};'
        self.log.info(view)
        res = self.comsh.execut_db_sql(view)
        self.log.info(res)

        self.assertIn(self.constant.CREATE_VIEW_SUCCESS_MSG, res,
                      '执行失败' + text)

        text = '-----step5:节点A读取页面P1,节点A和B共享内存中无P1(主节点查询);' \
               'expect:成功(内存中无数据,从磁盘获取)-----'
        self.log.info(text)

        sql = f'''select count(*) from {self.v_name};
            select count(*) from {self.t_name};'''
        self.log.info(sql)
        res_sql = self.comsh.execut_db_sql(sql)
        self.log.info(res_sql)

        self.assertEqual('31', res_sql.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '-----step6:对数据做修改后进行查询; expect:查看成功-----'
        self.log.info(text)

        sql = f'''delete from {self.t_name} where id < 30;
            update {self.t_name} set name='new_local' where id=33;
            select count(*) from {self.t_name};'''
        self.log.info(sql)
        sql_res = self.comsh.execut_db_sql(sql)
        self.log.info(sql_res)

        self.assertEqual('21', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

    def tearDown(self):
        text = '-----step7:清理环境,删除表; expect:清理环境成功-----'
        self.log.info(text)

        clear = f'''drop table {self.t_name} cascade;'''
        self.log.info(clear)
        clear_res = self.comsh.execut_db_sql(clear)
        self.log.info(clear_res)

        self.assertIn(self.constant.DROP_TABLE_SUCCESS, clear_res,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
